import 'dart:io';
import 'package:path_provider/path_provider.dart';

import 'flutter_luban.dart';

class UploadImageUtils {
  static Future luBanFilePath(File imageFile) async {
    //使用luban进行压缩
    var quality = 100;
    if (imageFile.lengthSync() > 4 * 1024 * 1024) {
      quality = 50;
    } else if (imageFile.lengthSync() > 2 * 1024 * 1024) {
      quality = 60;
    } else if (imageFile.lengthSync() > 1 * 1024 * 1024) {
      quality = 70;
    } else if (imageFile.lengthSync() > 0.5 * 1024 * 1024) {
      quality = 80;
    } else if (imageFile.lengthSync() > 0.25 * 1024 * 1024) {
      quality = 90;
    }
    final tempDir = await getTemporaryDirectory();
    CompressObject compressObject = CompressObject(
      imageFile: imageFile,
      //image
      path: tempDir.path,
      //compress to path
      quality: quality,
      //first compress quality, default 60
      step: 9,
      //compress quality step, The bigger the fast, Smaller is more accurate, default 6
      mode: CompressMode.AUTO, //default AUTO
    );

    return await Luban.compressImage(compressObject).then((filePath) async {
      return filePath;
    });
  }
}
